#!/bin/bash

# Takes the number of clients as its argument
function run_with_clients {
    NUMBER_OF_CLIENTS=$1
    
    # Fill database
    if [ $DATABASE == "rethinkdb" ]; then
        ../../build/release/rethinkdb create $SSD_DRIVES --force
        ./dbench --force                                                                                        \
            -d "/tmp/insert_setup_out" -H $SERVER_HOSTS            \
            {server}rethinkdb:"-c 12 -m 32768 $SSD_DRIVES"                                              \
            {client}stress[$STRESS_CLIENT]:"-b 8-32 -v 8-32 -c $NUMBER_OF_CLIENTS -d 2000000i -w 0/0/1/0 -o $TMP_KEY_FILE"     \
            iostat:1 vmstat:1 rdbstat:1
    elif [ $DATABASE == "mysql" ]; then
        if [ "$NUMBER_OF_CLIENTS" -lt "1025" ]; then
            ./dbench --force                                                                                         \
                -d "/tmp/insert_setup_out" -H $SERVER_HOSTS             \
                {server}mysql:"$MYSQL_COMMON_FLAGS $MYSQL_BUFFER_FLAGS $MYSQL_DURABILITY_FLAGS  $MYSQL_SSD_FLAGS"              \
                {client}mysqlstress[$STRESS_CLIENT]:"-b 8-32 -v 8-32 -c $NUMBER_OF_CLIENTS -d 2000000i -w 0/0/1/0 -o $TMP_KEY_FILE" \
                iostat:1 vmstat:1
        fi
    elif [ $DATABASE == "membase" ]; then
        ./dbench --force                                                                                   \
            -d "/tmp/insert_setup_out" -H $SERVER_HOSTS -p 11211 \
            {server}membase:"-d $PERSISTENT_DATA_DIR -m 32768"                                       \
            {client}stress[$STRESS_CLIENT]:"-b 8-32 -v 8-32 -c $NUMBER_OF_CLIENTS -d 2000000i -w 0/0/1/0 -o $TMP_KEY_FILE" \
            iostat:1 vmstat:1
    fi
    
    # Run benchmark
    if [ $DATABASE == "rethinkdb" ]; then
        ./dbench                                                                                                \
            --prime-caches                                                                                      \
            -d "$BENCH_DIR/bench_output/Number_of_clients_on_canonical_hardware_select_only_workload/$NUMBER_OF_CLIENTS" -H $SERVER_HOSTS    \
            {server}rethinkdb:"-c 12 -m 32768 $SSD_DRIVES"                                            \
            {client}stress[$STRESS_CLIENT]:"-b 8-32 -v 8-32 -c $NUMBER_OF_CLIENTS -d $CANONICAL_MULTIRUN_DURATION -w 0/0/0/1 -i $TMP_KEY_FILE" \
            iostat:1 vmstat:1 rdbstat:1
    elif [ $DATABASE == "mysql" ]; then
        if [ "$NUMBER_OF_CLIENTS" -lt "1025" ]; then
            ./dbench                                                                                                \
                --prime-caches                                                                                   \
                -d "$BENCH_DIR/bench_output/Number_of_clients_on_canonical_hardware_select_only_workload/$NUMBER_OF_CLIENTS" -H $SERVER_HOSTS \
                {server}mysql:"$MYSQL_COMMON_FLAGS $MYSQL_BUFFER_FLAGS $MYSQL_DURABILITY_FLAGS $MYSQL_SSD_FLAGS"        \
                {client}mysqlstress[$STRESS_CLIENT]:"-b 8-32 -v 8-32 -c $NUMBER_OF_CLIENTS -d $CANONICAL_MULTIRUN_DURATION -w 0/0/0/1 -i $TMP_KEY_FILE" \
                iostat:1 vmstat:1
        else
            echo "Not running MySQL for more than 1024 clients"
        fi
    elif [ $DATABASE == "membase" ]; then
        ./dbench                                                                                                \
            --prime-caches                                                                                   \
            -d "$BENCH_DIR/bench_output/Number_of_clients_on_canonical_hardware_select_only_workload/$NUMBER_OF_CLIENTS" -H $SERVER_HOSTS -p 11211 \
            {server}membase:"-d $MEMBASE_DATA_PATH -m 32768"                                       \
            {client}stress[$STRESS_CLIENT]:"-b 8-32 -v 8-32 -c $NUMBER_OF_CLIENTS -d $CANONICAL_MULTIRUN_DURATION -w 0/0/0/1 -i $TMP_KEY_FILE" \
            iostat:1 vmstat:1
    else
        echo "No workload configuration for $DATABASE"
    fi
    
    # Cleanup
    if [ $DATABASE == "membase" ]; then
        rm -rf $PERSISTENT_DATA_DIR
    fi
    rm -rf /tmp/insert_setup_out
}

